const joi = require('joi');
const validationMiddleware = require('../../middleware/validate');
const { dealNotEmpty } = require('../../utils/dberrordeal');
const OrderController = require('../../controllers/good/orderController');
const router = require('koa-router')();
router.prefix('/order');
const { verify } = require('../../utils/auth');
router.use(verify);
const rulesSchema = {
    '/order/addorder': joi.object({
        goodsInfo: joi.array().items(joi.object({
            goodId: dealNotEmpty('商品id'),
            sid: dealNotEmpty('分组id'),
            quantity: joi.number().integer().min(0).required().messages({
                'number.base': '数量必须是数字',
                'number.integer': '数量必须是整数',
                'number.min': '数量不能小于0',
                'any.required': '数量是必填字段',
            }),
        })).required(),
        // couponId: dealNotEmpty('优惠券id'),
        addressId: dealNotEmpty('地址id'),
        totalPrice: joi.number().min(0).required().messages({
            'number.base': '总价必须是数字',
            'number.min': '总价不能小于0',
            'any.required': '总价是必填字段',
        }),
        status: joi.number().integer().min(0).required().messages({
            'number.base': '状态必须是数字',
            'number.integer': '状态必须是整数',
            'any.required': '状态是必填字段',
        }),
        paymentMethod: joi.number().integer().min(0).required().messages({
            'number.base': '支付方式必须是数字',
            'number.integer': '支付方式必须是整数',
            'any.required': '支付方式是必填字段',
        }),

    }),
    '/order/putorder': joi.object({
        goodsInfo: joi.array().items(joi.object({
            goodId: dealNotEmpty('商品id'),
            sid: dealNotEmpty('分组id'),
            quantity: joi.number().integer().min(0).required().messages({
                'number.base': '数量必须是数字',
                'number.integer': '数量必须是整数',
                'number.min': '数量不能小于0',
                'any.required': '数量是必填字段',
            }),
        })).required(),
        // couponId: dealNotEmpty('优惠券id'),
        addressId: dealNotEmpty('地址id'),
        totalPrice: joi.number().min(0).required().messages({
            'number.base': '总价必须是数字',
            'number.min': '总价不能小于0',
            'any.required': '总价是必填字段',
        }),
        status: joi.number().integer().min(0).required().messages({
            'number.base': '状态必须是数字',
            'number.integer': '状态必须是整数',
            'any.required': '状态是必填字段',
        }),
        paymentMethod: joi.number().integer().min(0).required().messages({
            'number.base': '支付方式必须是数字',
            'number.integer': '支付方式必须是整数',
            'any.required': '支付方式是必填字段',
        }),

        id: dealNotEmpty('订单id'),
    }),
    '/order/getorder': joi.object({
        id: dealNotEmpty('订单id'),
    }),
    '/order/delorder': joi.object({
        id: dealNotEmpty('订单id'),
    }),
    '/order/updateOrderStatus': joi.object({
        outTradeNo: dealNotEmpty('订单号'),
        status: joi.number().integer().min(0).required().messages({
            'number.base': '状态必须是数字',
            'number.integer': '状态必须是整数',
            'any.required': '状态是必填字段',
        }),
    }),

    
    '/order/successPaid': joi.object({
        outTradeNo: dealNotEmpty('订单号'),
        userId: dealNotEmpty('用户id'),
        totalAmount: joi.number().min(0).required().messages({
            'number.base': '总价必须是数字',
            'number.min': '总价不能小于0',
            'any.required': '总价是必填字段', 
        })
    }),
}
router.post('/addorder', validationMiddleware(rulesSchema), OrderController.createOrder);
router.post('/putorder', validationMiddleware(rulesSchema), OrderController.updateOrder);
try {
    router.post('/getorder', validationMiddleware(rulesSchema), OrderController.getOrderById);
} catch (error) {
    console.error('Error in router.post:', error);
}


router.post('/delorder', validationMiddleware(rulesSchema), OrderController.deleteOrder);
router.post('/updateOrderStatus', validationMiddleware(rulesSchema), OrderController.updateOrderStatus);
router.post('/getorderlist', OrderController.getOrderList);
router.post('/pay', OrderController.pay);
router.post('/spay', async (ctx) => {
    await ctx.render('pay', { title: '支付' });
});
router.post('/successPaid',validationMiddleware(rulesSchema),OrderController.successPaid)
module.exports = router;